package com.foreveross.taskservice.application.impl.task.statsdetail;

import java.util.List;

import javax.annotation.Resource;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.Page;
import org.springframework.stereotype.Service;

import com.foreveross.taskservice.application.impl.Util.ModelTranEntityUtil;
import com.foreveross.taskservice.application.impl.convert.AirFreightChannelConvert;
import com.foreveross.taskservice.application.task.statsdetail.ITaskDetailApplication;
import com.foreveross.taskservice.application.task.statsdetail.vo.BaseChannelVO;
import com.foreveross.taskservice.application.task.statsdetail.vo.TaskServicePage;
import com.foreveross.taskservice.common.CacheDataSpace;
import com.foreveross.taskservice.common.TaskStatusEnum;
import com.foreveross.taskservice.common.model.AbstractTaskModel;
import com.foreveross.taskservice.core.domain.channel.GradChannelEntity;
import com.foreveross.taskservice.infra.db.HBaseRowModel;
import com.foreveross.taskservice.infra.ddd.TaskserviceEntityHibernateRepository;
import com.foreveross.taskservice.infra.entity.StaticHistoryHbaseEntity;
import com.foreveross.taskservice.infra.hbase.jpa.EntityHBaseRepository;
import com.foreveross.taskservice.infra.util.DateUtil;
import com.foreveross.taskservice.taskcache.ITaskCacheQuery;
import com.google.common.collect.Lists;
@Service
public class TaskDetailApplicationImpl extends TaskserviceEntityHibernateRepository implements ITaskDetailApplication{
	@Resource(name="redisTaskCacheQueryImpl")
	private ITaskCacheQuery taskCacheQuery;
	
	@Override
	public TaskServicePage<AbstractTaskModel> query(String channelCode, int status, String grabDateStr,int pageLen,int pageIndex) throws Exception {
		String todayStr=DateUtil.getNowDateStr("yyyy-MM-dd");
		GradChannelEntity channel=get(GradChannelEntity.class, channelCode);
		TaskServicePage<AbstractTaskModel> page = null;
		List<AbstractTaskModel> tasks=null;
		BaseChannelVO channelVO=new BaseChannelVO();
		if(grabDateStr.equals(todayStr)){//当前抓取日期，从redis读取数据
			pageLen=getPageLen(pageLen);
			pageIndex=getPageIndex(pageIndex);
			
			long totalSize=0;
			if(channel!=null){
				tasks=taskCacheQuery.queryTaskDetailInfo(CacheDataSpace.AirFreightSpace(), channelCode,TaskStatusEnum.getTaskStatusEnum(status),pageLen*pageIndex, pageLen);
				totalSize=taskCacheQuery.queryTaskSize(CacheDataSpace.AirFreightSpace(), channelCode,TaskStatusEnum.getTaskStatusEnum(status));
				page=new TaskServicePage<AbstractTaskModel>(pageIndex, totalSize, pageLen,tasks);
				channelVO.setChannelModel(AirFreightChannelConvert.convert(channel));
				channelVO.setChannelId(channel.getId());
				channelVO.setName(channel.getName());
				page.setChannel(channelVO);
				page.setTaskStatus(status);
			}
			
		}else{//历史数据从hbase读取
			String tableName="CRAWL_DETAIL_INFO";
			String channelIdStr=channelCode;
//			String channelIdStr=channelId>=100?(channelId+""):(channelId>=10?"0"+channelId:"00"+channelId);
			String statuStr=status<0?(Math.abs(status)<10?"00"+Math.abs(status):"0"+Math.abs(status)):(status<10?"10"+status:"1"+status);
			grabDateStr=grabDateStr.replaceAll("-", "");
			String statrtRowKey="00001"+channelIdStr+grabDateStr+statuStr+"00000",endRowKey="00001"+channelIdStr+grabDateStr+statuStr+"ZZZZZ";
			List<StaticHistoryHbaseEntity> hModsE=InstanceFactory.getInstance(EntityHBaseRepository.class).findByRowkey(StaticHistoryHbaseEntity.class, statrtRowKey, endRowKey);
			// List<HBaseRowModel>  hMods= InstanceFactory.getInstance(com.foreveross.taskservice.infra.db.HbaseManager.class).getMultRecord(tableName, statrtRowKey, endRowKey);
			List<HBaseRowModel>  hMods =Lists.newArrayList();
			//List<StaticHistoryHbaseEntity> T List<AbstractTaskModel>
			if(hModsE.size()>0 && channel!=null ){
				//反射解释模型
				ModelTranEntityUtil MEU=new ModelTranEntityUtil();
				tasks=MEU.convertEntity(hModsE,pageLen,pageIndex,channel);
				page=new TaskServicePage<AbstractTaskModel>(pageIndex, hMods.size(), pageLen,tasks);
				channelVO.setChannelModel(AirFreightChannelConvert.convert(channel));
				channelVO.setChannelId(channel.getId());
				channelVO.setName(channel.getName());
				page.setChannel(channelVO);
				page.setTaskStatus(status);
			}
		}
		
		return page;
	}
	
	
	
	
	private int getPageLen(int pageLen){
		if(pageLen<=0 || pageLen>TaskServicePage.DEFAULT_PAGE_MAXSIZE){
			return Page.DEFAULT_PAGE_SIZE;
		}
		return pageLen;
	}
	
	private int getPageIndex(int pageIndex){
		if(pageIndex<0){
			return 0;
		}
		return pageIndex;
	}




	@Override
	public GradChannelEntity getEntity(long id) {
		GradChannelEntity channel=get(GradChannelEntity.class, id);
		return channel;
	}
	


	
}
